using System;
using System.Collections.Generic;
using System.Xml;
using SSP.Api.Interfaces;

namespace SSP.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DirectorResponse : DirectorWrapper, IDeserializer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectorResponse"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public DirectorResponse(Director parent) : base(parent)
        {
        }

        /// <summary>
        /// Gets or sets the content.
        /// </summary>
        /// <value>The content.</value>
        public DirectorContent Content { get; set; }

        /// <summary>
        /// Gets or sets the contents.
        /// </summary>
        /// <value>The contents.</value>
        public DirectorContent[] Contents { get; set; }

        /// <summary>
        /// Gets or sets the error.
        /// </summary>
        /// <value>The error.</value>
        public string Error { get; set; }

        /// <summary>
        /// Gets or sets the galleries.
        /// </summary>
        /// <value>The galleries.</value>
        public DirectorGallery[] Galleries { get; set; }

        /// <summary>
        /// Gets or sets the gallery.
        /// </summary>
        /// <value>The gallery.</value>
        public DirectorGallery Gallery { get; set; }

        /// <summary>
        /// Gets or sets the users.
        /// </summary>
        /// <value>The users.</value>
        public DirectorUser[] Users { get; set; }

        /// <summary>
        /// Gets or sets the version.
        /// </summary>
        /// <value>The version.</value>
        public string Version { get; set; }

        /// <summary>
        /// Gets or sets the method.
        /// </summary>
        /// <value>The method.</value>
        public string Method { get; set; }

        /// <summary>
        /// Gets or sets the format.
        /// </summary>
        /// <value>The format.</value>
        public string Format { get; set; }

        /// <summary>
        /// Gets or sets the status.
        /// </summary>
        /// <value>The status.</value>
        public string Status { get; set; }

        /// <summary>
        /// Gets or sets the albums.
        /// </summary>
        /// <value>The albums.</value>
        public DirectorAlbum[] Albums { get; set; }

        /// <summary>
        /// Gets or sets the album.
        /// </summary>
        /// <value>The album.</value>
        public DirectorAlbum Album { get; set; }

        /// <summary>
        /// Gets or sets the count.
        /// </summary>
        /// <value>The count.</value>
        public int Count { get; set; }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>The size.</value>
        public int Size { get; set; }

        /// <summary>
        /// Gets or sets the upload_limit.
        /// </summary>
        /// <value>The upload_limit.</value>
        public string UploadLimit { get; set; }

        #region Implementation of IDeserializer

        /// <summary>
        /// Gets or sets the <see cref="System.String"/> with the specified property name.
        /// </summary>
        /// <value></value>
        public string this[string propertyName]
        {
            get { return _sDict[propertyName]; }
            set { _sDict[propertyName] = value; }
        }

        private readonly Dictionary<string, string> _sDict = new Dictionary<string, string>();

        /// <summary>
        /// Deserializes from an XmlReader and sets the object's properties.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public void DeserializeFromReader(XmlReader reader)
        {
            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "method":
                        Method = reader.ReadElementContentAsString();
                        break;
                    case "format":
                        Format = reader.ReadElementContentAsString();
                        break;
                    case "stat":
                        Status = reader.ReadElementContentAsString();
                        break;
                    case "version":
                        Version = reader.ReadElementContentAsString();
                        break;
                    case "error":
                        Error = reader.ReadElementContentAsString();
                        break;
                    case "upload_limit":
                        UploadLimit = reader.ReadElementContentAsString();
                        break;
                    case "album":
                        Album = new DirectorAlbum(Parent);
                        Album.DeserializeFromReader(reader);
                        break;
                    case "albums":
                        Albums = getAlbums(reader);
                        break;
                    case "gallery":
                        Gallery = new DirectorGallery(Parent);
                        Gallery.DeserializeFromReader(reader);
                        break;
                    case "galleries":
                        Galleries = getGaleries(reader);
                        break;
                    case "users":
                        Users = getUsers(reader);
                        break;
                    case "content":
                        Content = new DirectorContent(Parent);
                        Content.DeserializeFromReader(reader);
                        break;
                    case "contents":
                        Contents = getContents(reader);
                        break;
                    case "count":
                        Count = reader.ReadElementContentAsInt();
                        break;
                    case "size":
                        Size = reader.ReadElementContentAsInt();
                        break;
                    default:
                        System.Diagnostics.Trace.WriteLine("Unhandled xml element name '" + reader.Name + "'.");
                        _sDict[reader.Name] = reader.ReadElementContentAsString();
                        break;
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();
        }

        #endregion
    }
}